package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"fmt"
	"strconv"
	"time"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/sessions"
)

type Collection struct {
	Ctx iris.Context
}

//收藏学校
func (c *Collection) PostCollectionCollege() interface{} {
	channel := c.Ctx.PostValue("channel")
	collegeId := c.Ctx.PostValue("colledgeId")

	if channel == "" || collegeId == "" {
		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
	}

	var userId int64
	if channel == "wechat" {
		token := c.Ctx.GetHeader("token")
		if token == "" {
			return utils.CommonRes(1000, fmt.Sprintf("token为空"), "")
		}

		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错"), "")
		}

		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%s userId", channel))
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("连接数据库出错: %s", err), "")
	}

	collegeID, err := strconv.ParseInt(collegeId, 10, 64)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据类型转换有误 %s", err), "")
	}

	collectionCollege := new(models.CollectionCollege)
	collectionCollege.UserId = userId
	collectionCollege.CollegeId = collegeID
	collectionCollege.Time = time.Now()
	collectionCollege.Status = 1

	_, err = db.Table(new(models.CollectionCollege)).Where("user_id = ?", userId).InsertOne(collectionCollege)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("插入数据出错: %s", err), "")
	}

	return utils.CommonRes(20000, "学校收藏成功", "")
}

//收藏专业
func (c *Collection) PostMajor() interface{} {
	channel := c.Ctx.PostValue("channel")
	categoryId := c.Ctx.PostValue("categoryId")

	if channel == "" || categoryId == "" {
		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
	}

	var userId int64
	if channel == "wechat" {
		token := c.Ctx.GetHeader("token")
		if token == "" {
			return utils.CommonRes(1000, fmt.Sprintf("token为空"), "")
		}

		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错"), "")
		}

		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%s userId", channel))
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("连接数据库出错: %s", err), "")
	}

	categoryID, err := strconv.ParseInt(categoryId, 10, 64)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据类型转换有误 %s", err), "")
	}

	collectionMajor := new(models.CollectionMajor)
	collectionMajor.UserId = userId
	collectionMajor.CategoryId = categoryID
	collectionMajor.Time = time.Now()
	collectionMajor.Status = 1

	_, err = db.Table(new(models.CollectionMajor)).Where("user_id = ?", userId).InsertOne(collectionMajor)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("插入数据出错: %s", err), "")
	}

	return utils.CommonRes(20000, "专业收藏成功", "")
}

//取消收藏学校
func (c *Collection) PostCancelCollege() interface{} {
	channel := c.Ctx.PostValue("channel")
	colledgeId := c.Ctx.PostValue("colledgeId")

	if channel == "" || colledgeId == "" {
		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
	}

	var userId int64
	if channel == "wechat" {
		token := c.Ctx.GetHeader("token")
		if token == "" {
			return utils.CommonRes(1000, fmt.Sprintf("token为空"), "")
		}

		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错"), "")
		}

		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%s userId", channel))
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("连接数据库出错: %s", err), "")
	}

	updateData := map[string]interface{}{
		"status": 0,
	}

	_, err = db.Table(new(models.CollectionCollege)).Where("user_id = ?", userId).Update(updateData)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据库有问题"), "")
	}

	return utils.CommonRes(20000, fmt.Sprintf("取消收藏成功"), "")
}

//取消收藏专业
func (c *Collection) PostCancelMajor() interface{} {
	channel := c.Ctx.PostValue("channel")
	categoryId := c.Ctx.PostValue("categoryId")

	if channel == "" || categoryId == "" {
		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
	}

	var userId int64
	if channel == "wechat" {
		token := c.Ctx.GetHeader("token")
		if token == "" {
			return utils.CommonRes(1000, fmt.Sprintf("token为空"), "")
		}

		claims, err := utils.ParseToken(token)
		if err != nil {
			return utils.CommonRes(1000, fmt.Sprintf("解析token出错"), "")
		}

		userId = utils.Atoi64(claims.Id)
	} else {
		session := sessions.Get(c.Ctx)
		userId, _ = session.GetInt64(fmt.Sprintf("%s userId", channel))
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("连接数据库出错: %s", err), "")
	}

	updateData := map[string]interface{}{
		"status": 0,
	}

	_, err = db.Table(new(models.CollectionMajor)).Where("user_id = ?", userId).Update(updateData)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("数据库有问题"), "")
	}

	return utils.CommonRes(20000, fmt.Sprintf("取消收藏成功"), "")
}

//查看收藏学校列表  缺数据
// func (c *Collection) GetColleageList() interface{} {
// 	channel := c.Ctx.URLParam("channel")
// 	page := c.Ctx.URLParam("page")
// 	pageSize := c.Ctx.URLParam("pageSize")

// 	if channel == "" || page == "" || pageSize == "" {
// 	   return utils.CommonRes(1000, fmt.Sprintf("参数有问题"))
// 	}

// 	var userId int64
// 	if channel == "wechat" {
// 		token := c.Ctx.GetHeader("token")
// 		if token == "" {
// 			return utils.CommonRes(1000, fmt.Sprintf("token为空"), "")
// 		}

// 		claims,err := utils.ParseToken(token)
// 		if err != nil {
// 			return utils.CommonRes(1000, fmt.Sprintf("解析token出错"), "")
// 		}

// 		userId = utils.Atoi64(claims.Id)
// 	} else {
// 	  session := sessions.Get(c.Ctx)
// 	  userId, _ = session.GetInt64(fmt.Sprintf("%s userId", channel))
// 	}

// 	//连接数据库
// 	db, err := datasourse.NewMasterOrm()
// 	if err != nil {
// 		return utils.CommonRes(1000, fmt.Sprintf("连接数据库出错: %s", err), "")
// 	}

// 	start := (curPage - 1) * conf.ListDataConfig.PerPage

// 	_, err := db.Table(new(models.CollectionCollege)).Where("user_id = ?", userId).Limit(conf.ListDataConfig.PerPage, start)

// }

//查看专业列表
// func (c *Collection) GetMajorList() interface{} {
// 	channel := c.Ctx.URLParam("channel")
// 	categoryId := c.Ctx.URLParam("categoryId")
// 	page := c.Ctx.URLParam("page")

// 	if channel == "" || categoryId == "" || page == "" {
// 		return utils.CommonRes(1000, fmt.Sprintf("参数有误"), "")
// 	}

// 	//连接数据库
// 	db, err := datasourse.NewMasterOrm()
// 	if err != nil {
// 		return utils.CommonRes(1000, fmt.Sprintf("连接数据库出错: %s", err), "")
// 	}

// 	var userId int64
// 	if channel == "wechat" {
// 		token := c.Ctx.GetHeader("token")
// 		if token == "" {
// 			return utils.CommonRes(1000, fmt.Sprintf("token为空"), "")
// 		}

// 		claims, err := utils.ParseToken(token)
// 		if err != nil {
// 			return utils.CommonRes(1000, fmt.Sprintf("解析token出错"), "")
// 		}

// 		userId = utils.Atoi64(claims.Id)
// 	} else {
// 		session := sessions.Get(c.Ctx)
// 		userId, _ = session.GetInt64(fmt.Sprintf("%s userId", channel))
// 	}

// 	var majorIds []int64

// 	_, err = db.Table(new(models.CollectionMajor)).Where("user_id = ?", userId).Find(&majorIds)
// 	if err != nil {
// 		return utils.CommonRes(1000, fmt.Sprintf("获取专业id出错"), "")
// 	}

// 	var majorList []models.Major
// 	var major models.Major

// 	start := (page - 1) * conf.ListDataConfig.PerPage

// 	for _, v := range majorIds {
// 		_, err := db.Table(new(models.Major)).Where("category_id = ?", v).Limit(conf.ListDataConfig.PerPage, start).Get(&major)
// 		if err != nil {
// 			return utils.CommonRes(1000, fmt.Sprintf("获取专业列表出错"), "")
// 		}

// 		majorList = append(majorList, major)
// 	}

// 	return utils.CommonRes(20000, fmt.Sprintf("获取专业列表成功"), majorList)
// }
